package com.pw.study.flow

import org.junit.Test

import scala.io.StdIn
import scala.util.control.Breaks

class Match {


  def main(args: Array[String]): Unit = {
    println("请输入数字：")
    Breaks.breakable {
      while (true) {
        val str: String = StdIn.readLine()
        println(m1_type(str))
        if (str.equals("exit")) {
          Breaks.break()
        }
      }
    }
  }

  @Test
  def hello_List(): Unit = {
    val arr: Array[List[Int]] = Array(List(0), List(1, 0), List(0, 0, 0), List(1, 0, 0), List(88))
    for (x <- arr) {
      println(m1_List(x))
    }

  }

  /**
   * 匹配List
   *
   * @param x
   * @return
   */
  def m1_List(x: Any) = x match {
    case List(0) => "0" //匹配List(0)
    case List(x, y) => x + "," + y //匹配有两个元素的List
    case List(0, _*) => "0 ..."
    case _ => "something else"

  }

  def hello_tuple: Unit = {
    val tuple: Array[Product] = Array((0, 1), (1, 0), (1, 1), (1, 0, 2))
    for (x <- tuple) {
      println(m1_type(x))
    }
  }

  /**
   * 匹配类型
   *
   * @param x
   */
  def m1_type(x: Any) = x match {
    case i: Int => println("这个是int类型。。")
    case y: String => println("这是String类型。。。。")
    case list: List[_] => println("这是List 类型。。。。")
    case arr: Array[Int] => println("这是Array 类型。。。")
    case other => println("这是其他什么 。。。。")
  }

  @Test
  def hello_obj: Unit = {
    val user: User = User("zhangsan", 20)
    println(m1_obj(user))
    val user2: User = User("zhangsan", 11)
    println(m1_obj(user2))
  }

  /**
   * 匹配对象
   *
   * @param x
   * @return
   */
  def m1_obj(x: Any) = x match {
    case User("zhangsan", 11) => "yes"
    case _ => "no"
  }

  /**
   * 匹配元组
   *
   * @param x
   * @return
   */
  def m1_tuple(x: Any) = x match {
    case (0, _) => "0 ..." //是第一个元素是0的元组
    case (y, 0) => "" + y + "0" // 匹配后一个元素是0的对偶元组
    case (a, b) => "" + a + " " + b
    case _ => "something else" //默认

  }

  @Test
  def hello_Array(): Unit = {
    val array: Array[Any] = Array(Array(0), Array(1, 0), Array(0, 1, 0),
      Array(1, 1, 0), Array(1, 1, 0, 1), Array("hello", 90))
    for (x <- array) {
      println(m1_Array(x))
    }
  }

  /**
   * 匹配数据
   *
   * @param x
   * @return
   */
  def m1_Array(x: Any) = x match {
    case Array(0) => "0" //匹配Array(0) 这个数组
    case Array(x, y) => x + "," + y //匹配有两个元素的数组，然后将将元素值赋给对应的x,y
    case Array(0, _*) => "以0开头的数组" //匹配以0开头和数组
    case _ => "something else"
  }

  /**
   * 匹配常量
   */
  def m1_c(num: Any) = num match {
    case 5 => "Int five"
    case "hello" => "Hello String "
    case true => "hello true"
    case 'c' => "Hello char"
    case other => "hello other： " + other
  }

  case class User(name: String, age: Int)

}
